﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Agents.Data
{
    /// <summary>
    /// Types of notifications
    /// </summary>
    public enum NotificationTypes
    {
        LACK_NOTIFICATION = 1,
        DEAD_STOCK_NOTIFICATION
    };

    /// <summary>
    /// Creteria to choose top five products
    /// </summary>
    public enum TopProductsCreteria
    {
        NUM_OF_ORDERS,
        SUM_ITEMS,
        MOST_RECENTLY_ORDERED
    };

    /// <summary>
    /// Time resolution for graphs
    /// </summary>
    public enum TimeResolution
    {
        MONTHS_IN_YEAR,
        DAYS_IN_MONTH
    };

    /// <summary>
    /// Extension methods for enums
    /// </summary>
    public static class Extensions
    {
        // Data Members

        private static Dictionary<NotificationTypes, String> notificationTypesStrings = 
            new Dictionary<NotificationTypes, string>() { 
                {NotificationTypes.LACK_NOTIFICATION, "מחסור במלאי"}, 
                {NotificationTypes.DEAD_STOCK_NOTIFICATION, "מלאי מת"}};

        // Methods 

        /// <summary>
        /// Returns the string representing the type
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static String GetStringRepresentation(this NotificationTypes type)
        {
            return notificationTypesStrings[type];
        }

        /// <summary>
        /// Returns the address of the client
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public static String GetAddress(this Clients client)
        {
            return client.Street + " " + client.StreetNum + ", " +
                client.City;
        }

        /// <summary>
        /// Returns the address of the client in the format City,Street,St.Num
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public static String GetAddressInESRIFormat(this Clients client)
        {
            return (client.City + "," + client.Street + "," +
                client.StreetNum).Replace("'", "");
        }

        /// <summary>
        /// Calculates the distance between to lat/long points and returns the 
        /// approximate distance in kilometers
        /// </summary>
        /// <param name="from">Point in long/lat decimal degrees</param>
        /// <param name="to">Point in long/lat decimal degrees</param>
        /// <returns>Distance in kilometers</returns>
        public static double CalcDistance(double fromX, double fromY, double toX, double toY)
        {
            // Earth radius in Km
            double rad = 6371;

            // Convert to radians
            double p1X = fromX / 180 * Math.PI;
            double p1Y = fromY / 180 * Math.PI;
            double p2X = toX / 180 * Math.PI;
            double p2Y = toY / 180 * Math.PI;

            return Math.Acos(Math.Sin(p1Y) * Math.Sin(p2Y) +
                Math.Cos(p1Y) * Math.Cos(p2Y) * Math.Cos(p2X - p1X)) * rad;
        }
    }
}